/*

  Munipack - command line interface

  Copyright © 2010-2011 F.Hroch (hroch@physics.muni.cz)

  This file is part of Munipack.

  Munipack is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  Munipack is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with Munipack.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "munipack.h"
#include <wx/wx.h>
#include <wx/app.h>
#include <wx/cmdline.h>
#include <iostream>

using namespace std;

wxIMPLEMENT_APP_CONSOLE(Munipack);


void Munipack::SetEnvironment()
{
  wxString xpath;
  wxGetEnv("PATH",&xpath);

  // set path for libexec
#ifdef MUNIPACK_LIBEXEC_PATH
  xpath = wxString(MUNIPACK_LIBEXEC_PATH) + wxString(":") + xpath;
#endif

  wxString libexecpath;
  if( wxGetEnv("MUNIPACK_LIBEXEC",&libexecpath) )
    xpath = libexecpath + wxString(":") + xpath;

  wxString binpath;
  if( wxGetEnv("MUNIPACK_BIN",&binpath) )
    xpath = binpath + wxString(":") + xpath;

  wxSetEnv("PATH",xpath.c_str());
}


bool Munipack::OnInit()
{
  const int nacts = 15;
  const char *actions[nacts] = { 0, "mdark", "mflat", "dark", "flat", "coloring", 
				    "ctrafo", "cone", "astrometry", "photometry",
				    "votable", "calibrate", "listing", "fits",
				    "kombine" };

  int act = ID_NULL;
  for(int i = 0; i < argc; i++)
    for(int j = 0; j < nacts; j++)
      if( argv[i] == actions[j] ) {
	act = j + ID_NULL;
      }

  MuniCmdLineParser cmd(act,argc,argv);
  OnInitCmdLine(cmd);

  int parse = cmd.Parse(false);

  if( parse == -1 ) {

    cmd.Usage();
    return false;
  }
  else if( parse == 0 ) {

    if( act != ID_NULL ) {

      SetEnvironment();
      Bind(wxEVT_END_PROCESS,&Munipack::OnFinish,this);

      switch(act) {
      case ID_MDARK:      return mdark(cmd);
      case ID_MFLAT:      return mflat(cmd);
      case ID_DARK:       return dark(cmd);
      case ID_FLAT:       return flat(cmd);
      case ID_COLORING:   return coloring(cmd);
      case ID_CTRAFO:     return ctrafo(cmd);
      case ID_CONE:       return cone(cmd);
      case ID_ASTROMETRY: return astrometry(cmd);
      case ID_PHOTOMETRY: return photometry(cmd);
      case ID_VOTABLE:    return votable(cmd);
      case ID_CALIBRATE:  return calibrate(cmd);
      case ID_LISTING:    return listing(cmd);
      case ID_FITS:       return fits(cmd);
      case ID_KOMBINE:    return kombine(cmd);
      default:            return false;

      }
    }
    else {

      if( cmd.Found("version") ) {
	cout << INFOTEXT << " " << XVERSION << ", " << COPYLEFT << endl;
	cout << INFOTEXTFULL << endl;
	cout << endl;
	cout << "Munipack comes with ABSOLUTELY NO WARRANTY." << endl;
	cout << "You may redistribute copies of Munipack" << endl;
	cout << "under the terms of the GNU General Public License." << endl;
	cout << "For more information about these matters, see the file named COPYING." << endl;
      }
      else
	cmd.Usage();

      return false;
    }
  }
  else { // parse < 1

    return false;
    
  }

  return false;
}


int Munipack::OnRun()
{
  wxAppConsole::OnRun();
  return pipe.GetExitCode();
}

void Munipack::OnFinish(wxProcessEvent& event)
{
  ExitMainLoop();
}
